/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.pacman.net.socket;

import sg.edu.nus.iss.pacman.net.URLConstants;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import sg.edu.nus.iss.pacman.dao.Maze;

/**
 *
 * @author Tabiul Mahmood, fredy
 */
public class ServletConnection {

    SocketClientListener listener;
    private int[][] maze = new int[21][17];
    private Maze newMaze = new Maze();
    private String mazename;
    private String serverURL;

    public ServletConnection(String serverURL, SocketClientListener listener) {
        this.listener = listener;
        this.serverURL = serverURL;
    }

    public void downloadMaze(String mazename) {
        this.mazename = mazename;
        Thread t = new Thread(new DownloadMazeRunnable(serverURL));
        t.start();
    }

    class DownloadMazeRunnable implements Runnable {

        private String serverURL;

        public DownloadMazeRunnable(String serverURL)
        {
            this.serverURL = serverURL;
        }

        public void run() {
            HttpConnection conn = null;
            try {
                conn = (HttpConnection) Connector.open(serverURL +
                    URLConstants.MAZE_DOWNLOAD_URL + mazename);
                if (conn.getResponseCode() == conn.HTTP_OK) {
                    InputStream mazeXML = conn.openInputStream();
                    SAXParserFactory factory = SAXParserFactory.newInstance();
                    SAXParser parser = factory.newSAXParser();
                    parser.parse(mazeXML, new MazeHandler());
                    newMaze.setMap(maze);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                    listener.received(newMaze);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        class MazeHandler extends DefaultHandler {

            private int row = 0;
            private int col = 0;
            private String value = null;

            public void startElement(java.lang.String uri,
                java.lang.String localName, java.lang.String qName, Attributes attributes) {
                if (qName.equals("maze")) {
                    newMaze.setMazeName(attributes.getValue("name"));
                }
                if (qName.equals("grid")) {
                    row = Integer.parseInt(attributes.getValue("row"));
                    col = Integer.parseInt(attributes.getValue("col"));
                }
            }

            public void characters(char ch[], int start, int length) {
                value = String.valueOf(ch, start, length);
            }

            public void endElement(java.lang.String uri, java.lang.String localName,
                java.lang.String qName) {
                if (qName.equals("grid")) {
                    if (value != null && value.length() != 0) {
                        maze[row][col] = Integer.parseInt(value);
                    }
                }
            }
        }
    }
}